//
//  MTCustomFromAccessoryView.swift
//  MT
//
//  Created by ooops on 2020/1/12.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import Eureka
import MTCategory

class MTCustomFromAccessoryView: UIToolbar, NavigationAccessory {

    lazy var doneButton: UIBarButtonItem = {
        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        btn.setTitle("完成", for: .normal)
        btn.setTitleColor(UIColor.text.x1d1a1a, for: .normal)
        btn.titleLabel?.font = PingFangSCMediumFont(14)
        btn.adjustsImageWhenHighlighted = false
        btn.addTarget(self, action: #selector(didTapDone), for: .touchUpInside)
        let item1 = UIBarButtonItem(customView: btn)
        return item1
    }()
    
    lazy var cancelButton: UIBarButtonItem = {
        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        btn.setTitle("取消", for: .normal)
        btn.setTitleColor(UIColor.text.x9b9b9b, for: .normal)
        btn.titleLabel?.font = PingFangSCMediumFont(14)
        btn.adjustsImageWhenHighlighted = false
        btn.addTarget(self, action: #selector(didTapDone), for: .touchUpInside)
        let item1 = UIBarButtonItem(customView: btn)
        return item1
    }()
    
    var previousButton: UIBarButtonItem!
    var nextButton: UIBarButtonItem!
    
    
    //(barButtonSystemItem: .done, target: self, action: #selector(didTapDone))
    private var fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
    private var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)

    public override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: 44.0))
        autoresizingMask = .flexibleWidth
        barStyle = .default
        isTranslucent = false
        fixedSpace.width = 22.0
        initializeChevrons()
        setItems([cancelButton, previousButton, fixedSpace, nextButton, flexibleSpace, doneButton], animated: false)
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    private func initializeChevrons() {
        var bundle = Bundle(for: NavigationAccessoryView.classForCoder())
        if let resourcePath = bundle.path(forResource: "Eureka", ofType: "bundle") {
            if let resourcesBundle = Bundle(path: resourcePath) {
                bundle = resourcesBundle
            }
        }

        var imageLeftChevron = UIImage(named: "back-chevron", in: bundle, compatibleWith: nil)
        var imageRightChevron = UIImage(named: "forward-chevron", in: bundle, compatibleWith: nil)
        // RTL language support
        imageLeftChevron = imageLeftChevron?.imageFlippedForRightToLeftLayoutDirection()
        imageRightChevron = imageRightChevron?.imageFlippedForRightToLeftLayoutDirection()
        

        //previousButton = UIBarButtonItem(image: imageLeftChevron, style: .plain, target: self, action: #selector(didTapPrevious))
        //nextButton = UIBarButtonItem(image: imageRightChevron, style: .plain, target: self, action: #selector(didTapNext))
        previousButton = UIBarButtonItem(image: nil, style: .plain, target: self, action: nil)
        nextButton = UIBarButtonItem(image: nil, style: .plain, target: self, action: nil)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {}

    public var doneClosure: (() -> ())?
    public var nextClosure: (() -> ())?
    public var previousClosure: (() -> ())?

    @objc private func didTapDone() {
        doneClosure?()
    }

    @objc private func didTapNext() {
        nextClosure?()
    }

    @objc private func didTapPrevious() {
        previousClosure?()
    }

    public var previousEnabled: Bool {
        get {
            return previousButton.isEnabled
        }
        set {
            previousButton.isEnabled = newValue
        }
    }

    public var nextEnabled: Bool {
        get {
            return nextButton.isEnabled
        }
        set {
            nextButton.isEnabled = newValue
        }
    }

}
